package com.rxnetwork.sample.mvp.toolbar;

import android.content.Context;
import android.support.v4.app.FragmentActivity;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.rxnetwork.sample.R;

/**
 * 顶部状态栏
 */
public class TopbarLayout extends RelativeLayout implements View.OnClickListener {

    private TextView mTitleTv;
    private ImageView mBackImg;
    private ImageView mRightImg;
    private ImageView mCloseImg;
    private TextView mRightTv;
    private ImageView mSecondRightImg;
    private int mTopbarBgColor;

    public TopbarLayout(Context context) {
        super(context);
        init(context);
    }

    public TopbarLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    public TopbarLayout(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context);
    }

    private void init(Context context) {
        View.inflate(context, R.layout.topbar_layout, this);
        mTitleTv = (TextView) findViewById(R.id.title_tv);
        mBackImg = (ImageView) findViewById(R.id.back_img);
        mCloseImg = (ImageView) findViewById(R.id.close_img);
        mRightImg = (ImageView) findViewById(R.id.right_img);
        mBackImg.setOnClickListener(this);
        mRightTv = (TextView) findViewById(R.id.right_tv);
        mSecondRightImg = (ImageView) findViewById(R.id.right_second_img);

//        view.setBackgroundColor(mTopbarBgColor);
    }


    /**
     * 隐藏返回键
     */
    public void hideLeftImg() {
        mBackImg.setVisibility(View.GONE);
    }

    public void setTitle(CharSequence text) {
        if (!TextUtils.isEmpty(text))
            mTitleTv.setText(text);
    }

    public void setTitle(int resId) {
        mTitleTv.setText(resId);
    }

    public void setLeftImgClickListener(OnClickListener listener) {
        mBackImg.setOnClickListener(listener);
    }

    public void setCloseImgShow(boolean isShow) {
        mCloseImg.setVisibility(isShow ? View.VISIBLE : View.GONE);
    }

    public void setCloseImgListener(OnClickListener listener) {
        mCloseImg.setOnClickListener(listener);
    }

    public int getTopbarBgColor() {
        return mTopbarBgColor;
    }

    public void setTopbarBgColor(int topbarBgColor) {
        this.mTopbarBgColor = topbarBgColor;
        this.setBackgroundColor(mTopbarBgColor);
    }

    /**
     * 设置右边图标的图片和事件
     *
     * @param resId
     * @param listener
     */
    public void setRightImg(int resId, OnClickListener listener) {
        mRightImg.setVisibility(View.VISIBLE);
        mRightImg.setImageResource(resId);
        mRightImg.setOnClickListener(listener);
    }

    public void setSecondRightImg(int resId, OnClickListener listener) {
        mSecondRightImg.setVisibility(View.VISIBLE);
        mSecondRightImg.setImageResource(resId);
        mSecondRightImg.setOnClickListener(listener);
    }

    public ImageView getSecondRightImg() {
        return mSecondRightImg;
    }

    /**
     * 设置右边的TextView的行为
     *
     * @param content
     * @param listener
     */
    public void setRightText(String content, OnClickListener listener) {
        mRightTv.setVisibility(View.VISIBLE);
        mRightTv.setText(content);
        mRightTv.setOnClickListener(listener);
    }

    /**
     * 隐藏右边的TextView
     */
    public void hideRightText() {
        mRightTv.setVisibility(View.GONE);
    }

    public void hideRightImg() {
        mRightImg.setVisibility(View.GONE);
    }

    /**
     * @return 返回右边的Tv用于更多属性控制
     */
    public TextView getRightTv() {
        return mRightTv;
    }

    public ImageView getLeftImg() {
        return mBackImg;
    }

    public void setLeftImg(int resId) {
        mBackImg.setImageResource(resId);
    }

    @Override
    public void onClick(View v) {
        if (getContext() instanceof FragmentActivity) {
            FragmentActivity activity = (FragmentActivity) getContext();
            //这里是取出我们返回栈存在Fragment的个数
            if (activity.getSupportFragmentManager().getBackStackEntryCount() > 0) {
                //取出我们返回栈保存的Fragment,这里会从栈顶开始弹栈
                activity.getSupportFragmentManager().popBackStack();
            } else {
                activity.onBackPressed();
            }
        }
    }
}
